using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using NewGameTypes;
using UnityEngine;

public class SelectTransportUIOld : MonoBehaviour
{
	public GameObject gameModePanel;

	[SerializeField]
	private SelectableButtonController selectableButtonController;

	[SerializeField]
	private SelectableButtonTransport exampleTransportButton;

	[SerializeField]
	private UIScrollView scroll;

	[SerializeField]
	private GameTypeSelectorCarInfo transportInfoPanel;

	[SerializeField]
	private GameModesUI controllerSelectMode;

	[SerializeField]
	private UILabel setectTransportLable;

	[SerializeField]
	private UICenterOnChild centerOnChild;

	private void OnEnable()
	{
		ClearScroll();
		StartCoroutine(InitTransportButton());
	}

	private void Start()
	{
		centerOnChild.onCenter = DisableCenterOnChild;
	}

	public void Play()
	{
		controllerSelectMode.StartMode();
	}

	public void Show(GameModesUI.TransportType typeTransport)
	{
		gameModePanel.SetActive(false);
		base.gameObject.SetActive(true);
		switch (typeTransport)
		{
		case GameModesUI.TransportType.car:
			setectTransportLable.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Game_Common/gts_selectCar"))) ? ScriptLocalization.Get("Game_Common/gts_selectCar") : "Select car");
			break;
		case GameModesUI.TransportType.tank:
			setectTransportLable.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Game_Common/gts_selectTank"))) ? ScriptLocalization.Get("Game_Common/gts_selectTank") : "Select tank");
			break;
		case GameModesUI.TransportType.titan:
			setectTransportLable.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Game_Common/gts_selectTitan"))) ? ScriptLocalization.Get("Game_Common/gts_selectTitan") : "Select titan");
			break;
		default:
			setectTransportLable.text = "Select transport";
			break;
		}
	}

	public void Exit()
	{
		base.gameObject.SetActive(false);
		gameModePanel.SetActive(true);
	}

	private IEnumerator InitTransportButton()
	{
		GameModesUI.TransportType transportType = controllerSelectMode.SelectableButtonController.GetSelectableButton().ModeInfo.typeTransport;
		List<ProductCar> transportList = new TransportList().GetActualTransportList(transportType);
		string selectedTransportName = settings.GetSelectedTransportName(transportType);
		bool isSelectedButton = false;
		foreach (ProductCar element in transportList)
		{
			SelectableButtonTransport tmpButton = Object.Instantiate(exampleTransportButton);
			tmpButton.gameObject.SetActive(true);
			selectableButtonController.AddNewButton(tmpButton);
			tmpButton.transform.SetParent(selectableButtonController.grid.transform);
			tmpButton.transform.localScale = Vector3.one;
			tmpButton.TransportProduct = element;
			tmpButton.TransportType = transportType;
			tmpButton.SetName((!string.IsNullOrEmpty(ScriptLocalization.Get("Market/" + element.id + "_title"))) ? ScriptLocalization.Get("Market/" + element.id + "_title") : "Нет локализации");
			Texture productTexture = null;
			ProductVehicleSkin skinProduct = MarketController.Instance.GetEquipedVehicleSkin(element.id);
			if (skinProduct != null)
			{
				productTexture = Resources.Load<Texture>("AtlasShopGuns/ASG_" + skinProduct.skinTextureName);
			}
			if (productTexture == null)
			{
				productTexture = Resources.Load<Texture>("ModeSelektor/" + element.texture);
			}
			tmpButton.SetTexture(productTexture as Texture2D);
			if (selectedTransportName.Equals(element.id))
			{
				tmpButton.Select(true);
				isSelectedButton = true;
			}
		}
		if (selectableButtonController.allButtons[0] != null && !isSelectedButton)
		{
			selectableButtonController.allButtons[0].Select(true);
		}
		selectableButtonController.grid.maxPerLine = GameModeManager.instance.GridMaxPerLine;
		selectableButtonController.grid.repositionNow = true;
		yield return null;
		CenterOnSelectedButton();
	}

	private void ClearScroll()
	{
		selectableButtonController.ClearAllButtons();
	}

	private void CenterOnSelectedButton()
	{
		centerOnChild.Recenter();
		scroll.ResetPosition();
		if (selectableButtonController.selectedButton != null)
		{
			int num = selectableButtonController.allButtons.IndexOf(selectableButtonController.selectedButton);
			if (num >= selectableButtonController.grid.maxPerLine * 2)
			{
				centerOnChild.CenterOn(selectableButtonController.selectedButton.transform);
			}
		}
	}

	private void DisableCenterOnChild(GameObject go = null)
	{
		centerOnChild.enabled = false;
	}
}
